package com.codeavatar.catools.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class CaReceiverHelper {

    private Context context;
    private BroadcastReceiver receiver;

    public CaReceiverHelper(Context context,BroadcastReceiver receiver){
        this.context = context;
        this.receiver = receiver;
    }

    /**
     *
     * @param action
     * @param broadcastPermission
     *  需要注册权限
     * <permission android:name="com.yuntoyun.codeavatar.app.exit" android:protectionLevel="signature" />
     * <uses-permission android:name="com.yuntoyun.codeavatar.app.exit" />
     */
    public void registerReceiver(String action,String broadcastPermission){
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);
        this.context.registerReceiver(this.receiver, filter, broadcastPermission, null);
    }

    public void sendBroadcast(String action,String broadcastPermission, Bundle extras){
        Intent intent = new Intent();
        intent.setAction(action);
        if(null != extras){
            intent.putExtras(extras);
        }
        this.context.sendBroadcast(intent, broadcastPermission);
    }

    public void unregisterReceiver(){
        this.context.unregisterReceiver(this.receiver);
    }
}
